<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
			<el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
				:class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="120px">
				<el-row :gutter="15">
					<el-col :span="12">
						<el-form-item label="车辆类型" prop="carTypes" :rules="[ {required: true, message:'车辆类型不能为空', trigger:'blur'} ]">
							<el-select v-model="inputForm.carTypes" multiple filterable allow-create default-first-option placeholder="请选择" style="width:100%">
							   <el-option v-for="item in $dictUtils.getDictList('car_type')" :key="item.value"
							   	:label="item.label" :value="item.value">
							   </el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="限行类型" prop="limitType" :rules="[ {required: true, message:'限行类型不能为空', trigger:'blur'} ]">
							<el-select v-model="inputForm.limitType" placeholder="请选择" style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('car_limit_type')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="开始时间" prop="begintime" :rules="[
						{ required: true, message: '请输入开始时间', trigger: 'blur', }, ]">
							<el-date-picker v-model="inputForm.begintime" style="width: 100%"
								format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="结束时间" prop="endtime" :rules="[
						{ required: true, message: '请输入结束时间', trigger: 'blur', }, ]">
							<el-date-picker v-model="inputForm.endtime" style="width: 100%"
								format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12"  v-if="inputForm.limitType==0&&inputForm.limitType!=''">
						<el-form-item label="单双号限号类型" prop="oddEvenType" :rules="[ {required: true, message:'单双号限号类型不能为空', trigger:'blur'} ]">
							<el-select v-model="inputForm.oddEvenType" placeholder="请选择" style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('car_oddeven_type')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周一限号" prop="monday" :rules="[ {required: true, message:'周一限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.monday" placeholder="请填写周一限号"></el-input> -->
							<el-select v-model="inputForm.monday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周二限号" prop="tuesday" :rules="[ {required: true, message:'周二限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.tuesday" placeholder="请填写周二限号"></el-input> -->
							<el-select v-model="inputForm.tuesday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周三限号" prop="wednesday" :rules="[ {required: true, message:'周三限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.wednesday" placeholder="请填写周三限号"></el-input> -->
							<el-select v-model="inputForm.wednesday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周四限号" prop="thursday" :rules="[ {required: true, message:'周四限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.thursday" placeholder="请填写周四限号"></el-input> -->
							<el-select v-model="inputForm.thursday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周五限号" prop="friday" :rules="[ {required: true, message:'周五限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.friday" placeholder="请填写周五限号"></el-input> -->
							<el-select v-model="inputForm.friday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周六限号" prop="saturday" :rules="[ {required: true, message:'周六限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.saturday" placeholder="请填写周六限号"></el-input> -->
							<el-select v-model="inputForm.saturday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="inputForm.limitType==1&&inputForm.limitType!=''">
						<el-form-item label="周日限号" prop="sunday" :rules="[ {required: true, message:'周日限号不能为空', trigger:'blur'} ]">
							<!-- <el-input v-model="inputForm.sunday" placeholder="请填写周日限号"></el-input> -->
							<el-select v-model="inputForm.sunday" multiple filterable allow-create default-first-option placeholder="请选择">
							   <el-option v-for="(item,index) in numbers" :key="index" :label="item" :value="item"></el-option>
							 </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备注信息" prop="remarks" :rules="[ ]">
							<el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button size="small" @click="visible = false">关闭</el-button>
				<el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定
				</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				numbers:[0,1,2,3,4,5,6,7,8,9],
				title: '',
				method: '',
				visible: false,
				loading: false,
				inputForm: {
					id: '',
					carTypes: [],
					limitType: '',
					oddEvenType: '',
					monday: '',
					tuesday: '',
					wednesday: '',
					thursday: '',
					friday: '',
					saturday: '',
					sunday: '',
					remarks: '',
					begintime: '',
					endtime: '',
				}
			}
		},
		components: {},
		methods: {
			init(method, id) {
				this.method = method
				this.inputForm.id = id
				if (method === 'add') {
					this.title = `新建车辆限号规定`
				} else if (method === 'edit') {
					this.title = '修改车辆限号规定'
				} else if (method === 'view') {
					this.title = '查看车辆限号规定'
				}
				this.visible = true
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					if (method === 'edit' || method === 'view') { // 修改或者查看
						this.loading = true
						this.$http({
							url: `/dlyrl/oa/carinfolimit/oaCarinfoLimit/queryById?id=${this.inputForm.id}`,
							method: 'get'
						}).then(({
							data
						}) => {
							this.inputForm = this.recover(this.inputForm, data.oaCarinfoLimit)
							this.loading = false;
							this.inputForm.monday=this.inputForm.monday.split(',');
							this.inputForm.tuesday=this.inputForm.tuesday.split(',');
							this.inputForm.wednesday=this.inputForm.wednesday.split(',');
							this.inputForm.thursday=this.inputForm.thursday.split(',');
							this.inputForm.friday=this.inputForm.friday.split(',');
							this.inputForm.saturday=this.inputForm.saturday.split(',');
							this.inputForm.sunday=this.inputForm.sunday.split(',');
							
						})
					}
				})
			},
			// 表单提交
			doSubmit() {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						if(this.inputForm.limitType==1&&this.inputForm.limitType!=''){
							this.inputForm.monday=this.inputForm.monday.join(',');
							this.inputForm.tuesday=this.inputForm.tuesday.join(',');
							this.inputForm.wednesday=this.inputForm.wednesday.join(',');
							this.inputForm.thursday=this.inputForm.thursday.join(',');
							this.inputForm.friday=this.inputForm.friday.join(',');
							this.inputForm.saturday=this.inputForm.saturday.join(',');
							this.inputForm.sunday=this.inputForm.sunday.join(',');
						}
						this.loading = true;
						this.$http({
							url: `/dlyrl/oa/carinfolimit/oaCarinfoLimit/save`,
							method: 'post',
							data: this.inputForm
						}).then(({
							data
						}) => {
							this.loading = false
							if (data && data.success) {
								this.visible = false
								this.$message.success(data.msg)
								this.$refs.inputForm.resetFields();
								this.$emit('refreshDataList')
							}
						})
					}
				})
			}
		}
	}
</script>
